using System.Globalization;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UIElements;
using System.Collections.Generic;
using System.Collections;
//随机生成可破坏瓦片
public class MapController : MonoBehaviour
{
    public static MapController instance;
    [Range(0f, 1f)]
    public float itemSpawnChance = 0.2f;//生成瓦片的概率
    public Tilemap map;
    // 瓦片资源基类通过它可以得到瓦片资源
    public TileBase tileBase;
    private List<Vector3> emptyPosition;//保存所有的空位
    private List<Vector3> wallPosition;//保存所有的可破坏墙的位置

    public GameObject enemy;//怪物预制体
    public GameObject door;//门预制体
    public GameObject playerData;//主角预制体
    private int mapWidth;
    private int mapHeight;
    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            if (instance != this)
            {
                Destroy(gameObject);
            }
        }
        DontDestroyOnLoad(gameObject);
        emptyPosition = new List<Vector3>();
        wallPosition = new List<Vector3>();
        mapWidth = 16;
        mapHeight = 8;
    }

    public void Init()
    {
        Debug.Log("随机生成可破坏瓦片");
        // 1.清空瓦片地图
        map.ClearAllTiles();
        wallPosition.Clear();
        emptyPosition.Clear();
        for (int i = 0; i <= mapWidth; i++)
        {
            for (int j = 0; j <= mapHeight; j++)
            {
                //跳过砖块
                if (i % 2 != 0 && j % 2 != 0)
                {
                    continue;
                }
                Vector3 position = new Vector3(-7.5f + i, 3.5f - j, 0);
                Vector3Int cell = map.WorldToCell(position);  // 将世界坐标转换为Tilemap的单元格坐标
                if (Random.value < itemSpawnChance)
                {
                    map.SetTile(cell, tileBase); // 设置
                    wallPosition.Add(position);
                }
                else
                {
                    emptyPosition.Add(position);
                }
            }
        }

        addPlayer();
        addEnemy();
        addDoor();

        //清空地图的炸弹
        GameObject[] res = GameObject.FindGameObjectsWithTag("Bomb");
        foreach (GameObject item in res)
        {
            Destroy(item);
        }
        //清空地图的道具
        res = GameObject.FindGameObjectsWithTag("Props");
        foreach (GameObject item in res)
        {
            Destroy(item);
        }
    }

    //生成主角
    private void addPlayer()
    {
        Debug.Log("生成主角");
        GameObject player = GameObject.Find("Player(Clone)");
        if (player == null)
        {
            playerData = Resources.Load("Player/Player") as GameObject;
            player = Instantiate(playerData, transform.position, Quaternion.identity);
        }

        //生成在左下角
        player.transform.position = new Vector2(-7.5f, -4.5f);
        // //去除相邻的map
        map.SetTile(map.WorldToCell(new Vector2(-7.5f, -4.5f)), null);
        map.SetTile(map.WorldToCell(new Vector2(-6.5f, -4.5f)), null);
        map.SetTile(map.WorldToCell(new Vector2(-7.5f, -3.5f)), null);
        emptyPosition.Remove(new Vector3(-7.5f, -4.5f, 0));
        emptyPosition.Remove(new Vector3(-6.5f, -4.5f, 0));
        emptyPosition.Remove(new Vector3(-7.5f, -3.5f, 0));
    }

    //生成怪物
    private void addEnemy()
    {
        //清空地图上的所有敌人
        GameObject[] res = GameObject.FindGameObjectsWithTag("Enemy");
        foreach (GameObject item in res)
        {
            Destroy(item);
        }
        if (emptyPosition.Count > 0)
        {
            for (int i = 0; i < GameApp.instance.enemyCount; i++)
            {
                int index = Random.Range(0, emptyPosition.Count);
                Instantiate(enemy, emptyPosition[index], Quaternion.identity);
            }
        }
    }

    //生成门
    private void addDoor()
    {
        GameObject res = GameObject.Find("Door(Clone)");
        if (res != null)
        {
            Destroy(res);
        }
        int index = Random.Range(0, wallPosition.Count);
        Instantiate(door, wallPosition[index], Quaternion.identity);
    }
}
